package com.chenglu.android.cart.mvp.presenter;




import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.cart.bean.CartUpdateBean;
import com.chenglu.android.cart.mvp.contract.CartContract;
import com.chenglu.android.cart.mvp.model.CartModel;
import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import okhttp3.RequestBody;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/27 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @description presenter
 */
public class CartPresenter extends BasePresenter<CartContract.View> implements CartContract.CartPresenter {

     private  CartContract.CartModel model;

    public CartPresenter(){
        model = new CartModel();
    }



    @Override
    public void getCart(String token) {
        if(!isViewAttached()){
            return;
        }
        model.getCart(token)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CartBean>() {
                    //订阅
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                        LogUtils.e("qqqqqqq:"+"onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull CartBean cartBean) {
                        LogUtils.e("qqqqqqq:"+"onNext");
                        if(cartBean.getCode()==(Constant.HTTPCODE200)){
                            mView.onSuccess(cartBean);
                        }else if(cartBean.getCode()==(Constant.HTTPCODE400)) {
                            ToastUtils.show(cartBean.getMsg());
                        }else if(cartBean.getCode()==(Constant.HTTPCODE401)) {
                            //登录异常
                            mView.onError(cartBean.getMsg());
                            ToastUtils.show(cartBean.getMsg());
                        }else if(cartBean.getCode()==(Constant.HTTPCODE404)) {
                            ToastUtils.show(cartBean.getMsg());
                        }else if(cartBean.getCode()==(Constant.HTTPCODE500)) {
                            ToastUtils.show(cartBean.getMsg());
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                        LogUtils.e("qqqqqqq:"+"onComplete");

                    }
                });

    }

    @Override
    public void getCartUpadet(String token, RequestBody requestBody) {
        if(!isViewAttached()){
            return;
        }
        model.getCartUpadet(token,requestBody)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CartUpdateBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CartUpdateBean cartUpdateBean) {
                        if(cartUpdateBean.getCode()==(Constant.HTTPCODE200)){
                            mView.getCartUpadet(cartUpdateBean);
                        }else  {
                            ToastUtils.show(cartUpdateBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }

    @Override
    public void getCartRemove(String token, String ids) {
        if(!isViewAttached()){
            return;
        }
        model.getCartRemove(token,ids)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CartUpdateBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CartUpdateBean cartUpdateBean) {
                        if(cartUpdateBean.getCode()==(Constant.HTTPCODE200)){
                            mView.getCartRemove(cartUpdateBean);
                        }else  {
                            ToastUtils.show(cartUpdateBean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
